package cn.gjxblog.thread.example.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/1 16:01
 */
@Slf4j
public class ThreadPoolExample6 {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        ArrayList<Future<String>> resultList = new ArrayList<>();

        for(int i = 0; i < 10; i++){
            Future<String> future = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("call()方法被自动调用！！！    " + Thread.currentThread().getName());
                    //该返回结果将被Future的get方法得到
                    return "call()方法被自动调用，任务返回的结果是： " + Thread.currentThread().getName();
                }
            });
            resultList.add(future);
        }

        for (Future<String> fs : resultList) {
            try {
                while (!fs.isDone());
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}
